#!/bin/bash

function wordscounter()
{
	words=$#
	COUNTER=$[$COUNTER+$words]
	CHECK=$[$CHECK+1]
}

GO=0
NO_ARG=0
OPTIONERR="Errore opzione non corretta"
NOFILEERR="Errore: file mancanti!"
#Opzioni di "default"
ALL="ALL"
USR=$ALL
FROM=$ALL
TO=$ALL

COUNTER=1
CHECK=0
#controllo correttezza parametri
if [ $# -eq "$NO_ARG" ]
	then echo "Utilizzo: `basename $0` opzioni disponibili:(-uap)"
fi	

while  getopts "u:a:p:"  OPT; do
	#statements
case $OPT in
	u)
		if [ `expr index "$OPTARG" '-'` -eq 1 ]; then
			echo $OPTIONERR
			COUNTER=$[0]
		elif [ "$USR" = "$ALL" ]; then
			USR=$OPTARG
			wordscounter $USR
			USR=${USR//" "/"_"}
			CHECK=$[$CHECK+1]
			COUNTER=$[$COUNTER+1]		
		else
			COUNTER =$[0]
			echo $OPTIONERR
		fi
	;;
	a)
		if [ `expr index "$OPTARG" '-'` -eq 1 ]; then
			echo $OPTIONERR
			COUNTER=$[0]
		elif [ "$TO" = "$ALL" ]; then
			TO=$OPTARG
			wordscounter $TO
			TO=${TO//" "/"_"}
			CHECK=$[$CHECK+1]
			COUNTER=$[$COUNTER+1]
		else
			COUNTER =$[0]
			echo $OPTIONERR
		fi
	;;
	p)
		if [ `expr index "$OPTARG" '-'` -eq 1 ]; then
			echo $OPTIONERR
			COUNTER=$[0]
		elif [ "$FROM" = "$ALL" ]; then
			FROM=$OPTARG
			wordscounter $FROM
			FROM=${FROM//" "/"_"}
			CHECK=$[$CHECK+1]
			COUNTER=$[$COUNTER+1]
		else
			COUNTER =$[0]
			echo $OPTIONERR
		fi
	;;
	*) echo "Opzione non specificata!"
		COUNTER=$[0]
	;;
	esac
done
#Parametri script controllati, si controlla la correttezza dei file

if [ $CHECK -eq $# ]; then
	echo $NOFILEERR
	COUNT=$[0]
fi

if [ $COUNTER -e 0]; then
	exit 1
fi

#Ciclo principale
while [ $GO -eq 0 ]; do



done